#!/usr/bin/env python
#coding=utf-8
import Image

def make_thumb(path,dest,width,height=None):
    """
    缩略图生成程序
    """    
    try:
        im = Image.open(path)
    except IOError:
        return
    mode = im.mode
    if mode not in ('L', 'RGB'):
        if mode == 'RGBA':
            # 透明图片需要加白色底
            alpha = im.split()[3]
            bgmask = alpha.point(lambda x: 255-x)
            im = im.convert('RGB')
            im.paste((255,255,255), None, bgmask)
        else:
            im = im.convert('RGB')
            
    region = im
    if not height:
        src_width,src_height = im.size
        height = int(width * (float(src_height) / src_width))
    thumb = region.resize((width,height), Image.ANTIALIAS)
    thumb.save(dest, quality=75) # 默认 JPEG 保存质量是 75, 不太清楚。可选值(0~100)

if __name__ == '__main__':    
    make_thumb(r"/Users/jeff/Downloads/0011.jpg","/Users/jeff/Downloads/0011_fix.jpg",400)
    
